home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ctrlc.exe / READ.ME < prev   
Text File  |  1992-09-30  |  5KB  |  135 lines

  1.  
  2. Greetings,
  3.         I work for a small OEM of embedded process controls. This zip file
  4. contains 'CTRLC.C', the quick-n-dirty Ctrl-Break & Ctrl-C intercept we use in
  5. our systems, based on AT class motherboard. The file also contains the BIOS
  6. int 15h intercept we use. Most of the info was gleaned from The Programmers PC
  7. Sourcebook, by Thom Hogan, (the old blue one, not the error-ridden 2nd ed),
  8. and verified in the AT tech ref, MS-DOS programmer's guide, etc.
  9.  
  10.         Originally, we tried to handle Ctrl-Break via signal() calls.
  11. The code looked something like this:
  12.  
  13.         Boolean breakFlag = False;
  14.  
  15.         void breakHandler()
  16.             {
  17.             signal( SIGINT, breakHandler );
  18.             breakFlag = True;
  19.             }
  20.  
  21.         int main()
  22.             {
  23.             ...
  24.             signal( SIGINT, breakHandler );
  25.             ...
  26.             }
  27.  
  28. Problems with this approach were:
  29.  
  30.     1. The handler had to be re-installed at each invocation. We found that
  31.        it was possible to generate Ctrl-Break or Ctrl-C fast enough to
  32.        bypass the handler, and dump the user back to dos. In our systems,
  33.        this is a safety hazard, not a mere nuisance. (The custom co-processor
  34.        is not informed of the shutdown, interrupt vectors remain hooked, etc)
  35.  
  36.    2.  There was no way to differentiate between a Ctrl-Break and a Ctrl-C.
  37.        We are presently treating these keys identically, but someday ...
  38.  
  39.        We whipped up a quick-n-dirty ASM file to hook the BIOS int 1Bh, and
  40. DOS int 23h. We (belatedly) realized the three finger salute was another
  41. potential safety hazard, and added the AT BIOS int 15h intercept. Then PrtSc,
  42. then the SysRequst key. Later on, we decided to attempt to translate all ASM
  43. files over to (Borland extended) 'C', and from there to "all files compiled with
  44. the -P (CPP always) command switch". The (slightly modified) result is file
  45. CTRLC.C.
  46.  
  47.         The basics, in psuedo-ASM. Initialize by hooking interrupt vectors
  48. 0x1B and 0x23 to routines similar to these:
  49.  
  50. public  breakflag, new1Bh, new23h
  51.  
  52.         .data
  53. breakflag       dw 0
  54.  
  55.         .code
  56.  
  57. dGrp    dw @data
  58.  
  59. proc setFlag near
  60.         push    ds
  61.         mov     ds, [dGrp]      ; load flag segment
  62.         mov     [breakflag], 1  ; set flag to boolean True
  63.         pop     ds
  64.         ret
  65. endp setFlag
  66.  
  67. proc new1Bh far
  68.         call    setFlag         ; go set flag
  69.         iret                    ; simple return to BIOS
  70. endp new1Bh
  71.  
  72. proc new23h far
  73.         call    setFlag         ; go set flag
  74.  
  75. ;       sti                     ; this would work, usually, but sometimes
  76. ;       clc                     ;  failed under QEMM, EMM386, etc.
  77. ;       ret     2               ;  something to do with the interrupt flag
  78.                                 ;  in virtual 86 mode, we think
  79.  
  80.         push    bp              ; this is more convoluted, but worked under
  81.         mov     bp, sp          ;   QEMM, etc, without disturbing the int flag
  82.         push    ax
  83.         mov     ax, [bp+6]      ; fetch flags from stack
  84.         and     not 1           ; clearing carry tells DOS to continue
  85.         mov     [bp+6], ax      ; put adjusted flags back
  86.         pop     ax              ; restore & exit
  87.         pop     bp
  88.         iret
  89. endp new23h
  90.  
  91. The 'C' version of this formerly ASM file contains:
  92.  
  93.         void init_kybd();       // install interrupt intercepts
  94.         void term_kybd();       // restore original interrupt vectors
  95.         Boolean breakflag;      // set True when Ctrl-Break or Ctrl-C hit
  96.  
  97. Potential problems with our approach:
  98.  
  99.     1.  Supposedly, the infamous "Abort, Retry, Ignore, Fail" will abort
  100.         via int 23h. For us, that's not a problem, because we also intercept
  101.         int 24h (Critical Error), so we never abort, and never see this
  102.         message anyway. (See Borland's harderr(), hardretn(), etc)
  103.  
  104.     2.  In some cases that we haven't really chased down yet, MS-DOS v5.00
  105.         can and does remove a leading Ctrl-C from the keyboard buffer. This
  106.         has something to do with disk access, which is about the only dos calls
  107.         we make (Borland's memory calls during malloc(), etc are the others).
  108.         This is one reason we Ctrl-Break and Ctrl-C are still treated the same.
  109.  
  110.  
  111. Note that our systems are multitaskers (preemptive and cooperative, and a BEAR
  112. to debug sometimes !). The multitasker, window system, data input, serial
  113. drivers, etc were all developed in-house (except Borland's run-time, and
  114. Raima's dbVista). We access the hardware via BIOS when we can (keyboard, lpt's), directly when we need to
  115. (video, aux's), and via DOS as a last resort (disk). All keyboard input calls
  116. use the AT extensions to BIOS int 16h. Keyboard input is a relatively low
  117. priority task.
  118.  
  119.     !!   WHAT WORKS FOR US MAY NOT MEET YOUR NEEDS    !!
  120.  
  121. Obligatory legal stuff. These files are released into the public domain.
  122. You may use them, modify them, etc to your heart's content. The author (me)
  123. is NOT liable for any damages, lost profits, or any other problems caused by
  124. your use of these routines. They work for us, so far, but you USE AT YOUR OWN
  125. RISK !
  126.  
  127. (Whew. Ought to put that on a hot-key.)
  128.  
  129. Hope this is of some help. Questions are welcome, but may scroll off before
  130. I can catch them (another road trip tomorrow). If you don't get an answer,
  131. try again.
  132.  
  133. Roger Pittman [Jolly Roger :-)]
  134. CI$ 74130,3011
  135.